<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/15
 * Time: 10:07
 */

namespace backend\helpers;

use common\consts\Consts;
use common\helpers\UiHelper;
use common\models\Task;

//use common\models\Task;
use Yii;

class TaskUiHelper extends UiHelper
{
    public static function getLabelSpanByStatus($taskStatus, $labelText)
    {
        $statusMap = [
            Consts::TASK_STATUS_OPEN => self::$PRIMARY_CLASS,
            Consts::TASK_STATUS_RUNNING => self::STATUS_WARNING,
            Consts::TASK_STATUS_FINISHED => self::$SUCCESS_CLASS,
            Consts::TASK_STATUS_ERROR => self::$FAIL_CLASS,
            Consts::TASK_STATUS_TERMINATED => self::$MUTED_CLASS,
        ];
        $class = self::getValueByKeyInMap($statusMap, $taskStatus);
        return sprintf("<span class=\"label label-%s\">%s</span>", $class, $labelText);
    }

    public static function getLabelSpanByMdmTaskFlag($mdmTaskFlag, $labelText)
    {
        $statusMap = [
            Consts::MDM_TASK_FLAG_INIT => self::$PRIMARY_CLASS,
            Consts::MDM_TASK_FLAG_RUNNING => self::STATUS_WARNING,
            Consts::MDM_TASK_FLAG_SUCCESS => self::$SUCCESS_CLASS,
            Consts::MDM_TASK_FLAG_FAIL => self::$FAIL_CLASS,
        ];
        $class = self::getValueByKeyInMap($statusMap, $mdmTaskFlag);
        return sprintf("<span class=\"label label-%s\">%s</span>", $class, $labelText);
    }


    /**
     * @param $taskFinishPercentageStr integer
     */
    public static function getLabelSpanByPercentage($taskFinishPercentageStr)
    {
        if (!is_numeric ($taskFinishPercentageStr)) {
            $labelText = $taskFinishPercentageStr;
            $class = self::STATUS_IN_PROGRESS;
        }
        else {
            Yii::$app->formatter->decimalSeparator = Consts::DECIMAL_SEPARATOR;
            $labelText = Yii::$app->formatter->asPercent($taskFinishPercentageStr / 100, 2);

            if($taskFinishPercentageStr == 0) {
//            $class = self::$MUTED_CLASS;
                $class = self::STATUS_NO_STATED;
            }
            else if ($taskFinishPercentageStr < 100){
//            $class = self::$PRIMARY_CLASS;
                $class = self::STATUS_IN_PROGRESS;
            }
            else {
//            $class = self::$SUCCESS_CLASS;
                $class = self::STATUS_FINISH;
            }
        }

        return sprintf("<span class=\"badge bg-%s\">%s</span>", $class, $labelText);

//        $width = sprintf('width: %s', $labelText);
//        return sprintf("<div class=\"progress progress-xs progress-striped active\"><div class=\"progress-bar progress-bar-%s\" style=\"%s\"></div></div>", $class, $width);
    }
}